/**
 * 线性渐变起止方向的计算方法
 *
 *
 * @param {*} startArc 开始角度
 * @param {*} endArc 结束角度
 * @returns 四个坐标 x,y,x2,y2
 */
function getCoordinates(startArc, endArc) {
  // 将角度限制在 0 到 360 度之间
  startArc = ((startArc % 360) + 360) % 360
  endArc = ((endArc % 360) + 360) % 360

  const posi = [
    Math.sin((startArc * Math.PI) / 180),
    -Math.cos((startArc * Math.PI) / 180),
    Math.sin((endArc * Math.PI) / 180),
    -Math.cos((endArc * Math.PI) / 180)
  ]
  const dx = posi[2] - posi[0]
  const dy = posi[3] - posi[1]

  return getLocation(dx, dy)
}

function getLocation(dx, dy) {
  const tanV = dx / dy
  const directSign = Math.abs(tanV) < 1
  const t = directSign ? tanV : 1 / tanV

  const sign1 = t > 0 ? 1 : -1
  const sign2 = dx > 0 ? 1 : -1
  const sign = directSign ? sign1 * sign2 : sign2

  const [group1Start, group1End] = [0.5 - (sign * t) / 2, 0.5 + (sign * t) / 2]
  const [group2Start, group2End] = sign > 0 ? [0, 1] : [1, 0]

  return {
    x: group1Start,
    x2: group1End,
    y: group2Start,
    y2: group2End
  }
}

export default getCoordinates
